
import os
import shutil

rootDir = "/Users/mbc/Downloads/3333"
base_folder = os.path.join(rootDir, "baseAsset")
other_folder = os.path.join(rootDir, "otherAsset")

modifyCount = 0
findCount = 0

if os.path.exists(base_folder):
    print(f"===== path exist {base_folder}")
else:
    os.mkdir(base_folder)

if os.path.exists(other_folder):
    print(f"===== path exist {other_folder}")
else:
    os.mkdir(other_folder)


def modifyFilesInFolder(folder):
    global modifyCount
    global findCount
    # print(f"===== asset folders: {folder}")
    for dirName in os.listdir(folder):
        dirPath = os.path.join(folder, dirName)
        if os.path.isdir(dirPath):
            if dirPath.endswith(".imageset"):
                findCount += 1
                # print(f"===== asset: {dirPath}")
                find = 0
                for fileName in os.listdir(dirPath):
                    if fileName.find("@3x") != -1:
                        newName = dirName.replace(".imageset", "@3x.png")
                        os.renames(os.path.join(dirPath, fileName), os.path.join(dirPath, newName))
                        size = os.path.getsize(os.path.join(dirPath, newName))
                        if size < 5500:
                            shutil.copy(os.path.join(dirPath, newName), os.path.join(base_folder, newName))
                        else:
                            shutil.copy(os.path.join(dirPath, newName), os.path.join(other_folder, newName))
                        modifyCount += 1
                        find = 1
                        break

                if find == 0:
                    print(f"====== error folder: {dirPath}")
            else:
                # print(f"===== sub folders: {dirPath}")
                modifyFilesInFolder(dirPath)

for dirname in os.listdir(rootDir):
    dirPath = os.path.join(rootDir, dirname)
    if os.path.isdir(dirPath) and dirPath.endswith(".xcassets"):
        modifyFilesInFolder(dirPath)


print(f"====== find count: {findCount} == modify count: {modifyCount}")




